{#
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
#}

{% extends 'metastore_browser/base.html' %}

{% block plugin_content %}
<div>
    <h4>
        <span style="color:#AAA;">Table:</span>
        <span>{{ table.dbName }}.{{ table.tableName }}</span>
    </h4>
</div>
<ul class="nav nav-tabs" id="tabs" style="clear: both">
    <li role="presentation" class="active"><a href="#home" aria-controls="fields" role="tab" data-toggle="tab">Fields</a></li>
    <li role="presentation"><a href="#data" aria-controls="data" role="tab" data-toggle="tab">Sample Data</a></li>
    <li role="presentation"><a href="#partitions" aria-controls="partitions" role="tab" data-toggle="tab">Partitions</a></li>
    <li role="presentation"><a href="#attributes" aria-controls="attributes" role="tab" data-toggle="tab">Attributes</a></li>
    <li role="presentation"><a href="#parameters" aria-controls="parameters" role="tab" data-toggle="tab">Parameters</a></li>
    <li role="presentation"><a href="#ddl" aria-controls="ddl" role="tab" data-toggle="tab">DDL</a></li>
</ul>
<div class="tab-content">
  <div role="tabpanel" class="tab-pane fade in active" id="home">
    <table class="table table-striped table-bordered table-hover">
        <thead>
            <tr>
                <th>Column</th>
                <th>Type</th>
                <th>Comment</th>
            </tr>
        </thead>
        <tbody>
        {%for col in table.partitionKeys %}
            <tr>
                <td><b>{{ col.name }}</b> (PARTITION)</td>
                <td>{{ col.type }}</td>
                <td>{{ col.comment or '' }}</td>
            </tr>
        {% endfor %}
        {%for col in table.sd.cols %}
            <tr>
                <td>{{ col.name }}</td>
                <td>{{ col.type }}</td>
                <td>{{ col.comment or '' }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
  </div>
  <div role="tabpanel" class="tab-pane fade" id="data" style="white-space:nowrap; margin-top: 10px;">
      <img src="{{ url_for('static', filename='loading.gif') }}" width="50" style="margin: 20px">
  </div>
  <div role="tabpanel" class="tab-pane fade" id="partitions" style="white-space:nowrap; margin-top: 10px;">
      <img src="{{ url_for('static', filename='loading.gif') }}" width="50" style="margin: 20px">
  </div>
  <div role="tabpanel" class="tab-pane fade" id="attributes">
    <table class="table table-striped table-bordered table-hover">
        <thead>
            <tr>
                <th>Attribute</th>
                <th>Value</th>
            </tr>
        </thead>
        <tbody>
        {%for k, v in table.__dict__.items() %}
            {% if v and k not in ('sd', 'partitionKeys', 'tableName', 'parameters') %}
            <tr>
                <td>{{ k }}</td>
                {% if k.endswith('Time') %}
                    <td>{{ datetime.fromtimestamp(int(v)).isoformat() }}</td>
                {% else %}
                    <td>{{ v }}</td>
                {% endif %}
            </tr>
            {% endif %}
        {% endfor %}
        </tbody>
    </table>
  </div>
  <div role="tabpanel" class="tab-pane fade" id="parameters">
    <table class="table table-striped table-bordered table-hover">
        <thead>
            <tr>
                <th>Parameter</th>
                <th>Value</th>
            </tr>
        </thead>
        <tbody>
        {%for k, v in table.parameters.items() %}
            {% if v and k not in [] %}
            <tr>
                <td>{{ k }}</td>
                {% if k.endswith('Time') %}
                    <td>{{ datetime.fromtimestamp(int(v)).isoformat() }}</td>
                {% else %}
                    <td>{{ v }}</td>
                {% endif %}
            </tr>
            {% endif %}
        {% endfor %}
        </tbody>
    </table>
  </div>
  <div role="tabpanel" class="tab-pane fade" id="ddl">
      <pre id="ddl_content">
        <img src="{{ url_for('static', filename='loading.gif') }}" width="50" style="margin: 20px">
      </pre>
  </div>
</div>
{% endblock %}

{% block tail %}
{{ super() }}
<script>
$('#tabs a').click(function (e) {
    //e.preventDefault();
    $(this).tab('show');
});

$.get("{{ url_for(".data" , table=table_name) }}", function( data ) {
    $("#data").html(data);
    $('#data table.dataframe').dataTable({
        "iDisplayLength": 30,
    });
});

$.get("{{ url_for(".partitions" , table=table_name) }}", function( data ) {
    $("#partitions").html(data);
    $('#partitions table.dataframe').dataTable({
        "iDisplayLength": 30,
    });
});

$.get("{{ url_for(".ddl" , table=table_name) }}", function( data ) {
    $("#ddl_content").html(data);
});
</script>
{% endblock %}
